﻿using System.Data.Entity;
using System.Linq;
using Df.Framework.Common;
using Df.Common.Forms;

namespace DfMvc.Models
{
    public class DfDataContext : DbContext
    {
        public DfDataContext()
            : base("LocalDbConnection")
        {

        }
        public DbSet<DForm> DfForms { get; set; }
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Blog>().ToTable("Blogs");
            modelBuilder.Entity<Post>().ToTable("Posts");
            modelBuilder.Entity<DForm>().ToTable("DfForms");
            base.OnModelCreating(modelBuilder);
        }

        public override int SaveChanges()
        {
            //var modifiedEntries = this.ChangeTracker.Entries().Where(x => x.State == EntityState.Added || x.State == EntityState.Modified);

            //foreach (var dbEntityEntry in modifiedEntries)
            //{
            //    if (dbEntityEntry.Entity is DfBaseEntity)
            //    {
            //        var jsonData = JsonConvert.SerializeObject(dbEntityEntry.Entity);
            //        dbEntityEntry.Property("JsonData").CurrentValue = jsonData;
            //    }
            //}
            return base.SaveChanges();
        }

    }
}
